#include "Core/ResourceManager.h"
#include "Debug/Logger.h"

namespace blue {

SINGLETON_IMPL(ResourceManager)

ResourceManager::ResourceManager()
{
}

ResourceManager::~ResourceManager()
{
    if(_resources.size() > 0) Logger::log() << "Warning : some Resources have not been erased :" << "\n";
    for(std::vector<IResource*>::iterator it = _resources.begin(); it != _resources.end(); ++it)
        Logger::log() << (*it)->getName() << "\n";
}

void ResourceManager::add(IResource *resource)
{
    bool exist = false;
    for(unsigned i=0; i<_resources.size() && !exist; i++)
    {
        if(_resources[i]->getName() == resource->getName())
        {
            exist = true;
            Logger::log() << "Resource already added to the Resourcemanager : " << resource->getName() << "\n";
        }
    }
    if(!exist) _resources.push_back(resource);
}

void ResourceManager::remove(const std::string &name)
{
    for (std::vector<IResource*>::iterator it = _resources.begin(); it != _resources.end(); )
    {
        if((*it)->getName() == name)
            it = _resources.erase(it);
        else
            ++it;
    }
}

void ResourceManager::remove(IResource *resource)
{
    for (std::vector<IResource*>::iterator it = _resources.begin(); it != _resources.end(); )
    {
        if(*it == resource)
            it = _resources.erase(it);
        else
            ++it;
    }
}

}

